package com.ys.nms.module.business.service.devices;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import com.ys.nms.module.business.controller.admin.companyProperty.vo.CompanyPropertyPageReqVO;
import com.ys.nms.module.business.controller.admin.devices.vo.DevicesPageReqVO;
import com.ys.nms.module.business.controller.admin.devices.vo.DevicesSaveReqVO;
import com.ys.nms.module.business.dal.dataobject.companyproperty.CompanyPropertyDO;
import com.ys.nms.module.business.dal.dataobject.devices.DevicesDO;
import com.ys.nms.module.business.dal.mysql.devices.DevicesMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;

import java.util.*;
import java.util.stream.Collectors;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;

import static com.ys.nms.module.business.enums.ErrorCodeConstants.DEVICES_NOT_EXISTS;


/**
 * 设备 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DevicesServiceImpl implements DevicesService {

    @Resource
    private DevicesMapper devicesMapper;

    @Override
    public Integer createDevices(DevicesSaveReqVO createReqVO) {
        // 插入
        DevicesDO devices = BeanUtils.toBean(createReqVO, DevicesDO.class);
        devicesMapper.insert(devices);

        // 返回
        return devices.getDeviceId();
    }

    @Override
    public void updateDevices(DevicesSaveReqVO updateReqVO) {
        // 校验存在
        validateDevicesExists(updateReqVO.getDeviceId());
        // 更新
        DevicesDO updateObj = BeanUtils.toBean(updateReqVO, DevicesDO.class);
        devicesMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevices(Integer id) {
        // 校验存在
        validateDevicesExists(id);
        // 删除
        devicesMapper.deleteById(id);
    }

    @Override
    public void deleteDevicesListByIds(List<Integer> ids) {
        // 删除
        devicesMapper.deleteByIds(ids);
    }


    private void validateDevicesExists(Integer id) {
        if (devicesMapper.selectById(id) == null) {
            throw exception(DEVICES_NOT_EXISTS);
        }
    }

    @Override
    @TenantIgnore
    @Slave
    public DevicesDO getDevices(Integer id) {
        return devicesMapper.selectById(id);
    }

    @Slave
    @Override
    @TenantIgnore
    public PageResult<DevicesDO> getDevicesPage(DevicesPageReqVO pageReqVO) {
        return devicesMapper.selectPage(pageReqVO);
    }


    @Slave
    @TenantIgnore
    @Override
    public List<Integer> selectDeviceIds(CompanyPropertyPageReqVO pageReqVO) {
        LambdaQueryWrapperX<DevicesDO> deviceWrapper = new LambdaQueryWrapperX<DevicesDO>();
        deviceWrapper.eqIfPresent(DevicesDO::getStatus, pageReqVO.getStatus());
        deviceWrapper.eqIfPresent(DevicesDO::getDisabled, pageReqVO.getDisabled());
        deviceWrapper.select(DevicesDO::getDeviceId);
        List<DevicesDO> devicesList = devicesMapper.selectList(deviceWrapper);

        // 2. 提取设备ID列表
        List<Integer> deviceIds = devicesList.stream()
                .map(DevicesDO::getDeviceId)
                .collect(Collectors.toList());
        deviceIds.add(0);//防止空
        return deviceIds;
    }

    @Override
    @Slave
    public List<DevicesDO> getDevicesList() {
        return devicesMapper.selectList();
    }


}